package club.zhilin.guli.service.edu.service.impl;

import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.service.edu.domain.CourseCollect;
import club.zhilin.guli.service.edu.domain.vo.CourseCollectVO;
import club.zhilin.guli.service.edu.mapper.CourseCollectMapper;
import club.zhilin.guli.service.edu.service.ICourseCollectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author zhilin
 * @since 2022-01-11 15:29:01
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CourseCollectServiceImpl extends ServiceImpl<CourseCollectMapper, CourseCollect> implements ICourseCollectService {

    @Override
    public CommonResult<String> collect(String courseId, String id) {
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<CourseCollect>().eq(CourseCollect::getCourseId, courseId).eq(CourseCollect::getMemberId, id));
        if (count > 0) {
            return CommonResult.failed("该商品已经在收藏中");
        }
        CourseCollect courseCollect = new CourseCollect();
        courseCollect.setCourseId(courseId);
        courseCollect.setMemberId(id);
        boolean result = this.save(courseCollect);
        return result ? CommonResult.success("收藏成功") : CommonResult.failed("收藏失败");
    }

    @Override
    public CommonResult<List<CourseCollectVO>> selectList(String id) {
        List<CourseCollectVO> vos = baseMapper.selectCourseCollectVo(id);
        return CommonResult.success(vos);
    }

    @Override
    public CommonResult<String> delCollect(String courseId, String id) {
        boolean result = this.remove(new LambdaQueryWrapper<CourseCollect>().eq(CourseCollect::getCourseId, courseId).eq(CourseCollect::getMemberId, id));
        return result ? CommonResult.success("取消收藏成功") : CommonResult.failed("取消收藏失败");
    }
}
